#include "ULGfx_BallStatistic.h"
#include "UL_Global.h"
#include <SDL/sprig.h>

ULG_BallStatistic::ULG_BallStatistic():initFlags(0), ball(NULL)
{
}

ULG_BallStatistic::ULG_BallStatistic(ULG_Ball *ball):initFlags(0)
{
	SetULGBall(ball);
}

void ULG_BallStatistic::SetULGBall(ULG_Ball *ball)
{
	if (ball != NULL){
		this->ball = ball;
		initFlags |= ULG_BALLSTATISTIC_F_BALL_SET;
		ball->msg.SetFgColor(255, 255, 255);
	}
}

void ULG_BallStatistic::Show(SDL_Surface *destSurf, ULC_BallFrequency &ballStat, bool red, VSDL_Point point)
{
	return Show(destSurf, ballStat.number, ballStat.appearedTimes, ballStat.rate, red, point);
}

void ULG_BallStatistic::Show(SDL_Surface *destSurf, int ballNumber, int appearedTimes, float rate, bool red, VSDL_Point point)
{
	VSDL_Point currentXY = point;
	char msgBuffer[128];
	int barHeight;
	VSDL_RGB rgb;

	if (initFlags & ULG_BALLSTATISTIC_F_BALL_SET){
		//Draw ball
		ball->SetNumber(ballNumber);
		ball->Show(destSurf, currentXY);
		//Show tip
		currentXY.y += ball->ballHeight;
		sprintf(msgBuffer, "次数:%d", appearedTimes);
		UL_Global::UnionLottoShowTip(msgBuffer, currentXY);	
		currentXY.y += ball->fontHeight;
		sprintf(msgBuffer, "概率:%0.4f\%", rate);
		UL_Global::UnionLottoShowTip(msgBuffer, currentXY);	
		//Show bar
		rate /= 100;
		//Tripple rate
		rate *= 3;
		barHeight = ball->ballHeight + 2 * ball->fontHeight;
		currentXY = point;
		currentXY.x += 12 * ball->fontWidth;
		
		rgb.SetRGB(100, 100, 100);
		SPG_RectFilled(destSurf, currentXY.x, currentXY.y, currentXY.x + BAR_WIDTH, currentXY.y + barHeight, rgb.GetRGB32(destSurf));	
		
		if (red){
			rgb.SetRGB(250, 80, 80);
		}else{
			rgb.SetRGB(80, 80, 250);
		}

		SPG_RectFilled(destSurf, currentXY.x, currentXY.y + barHeight - (static_cast<int>(barHeight * rate))
				, currentXY.x + BAR_WIDTH, currentXY.y + barHeight, rgb.GetRGB32(destSurf));
	}
	
}

int ULG_BallStatistic::GetWidth()
{
	if (initFlags & ULG_BALLSTATISTIC_F_BALL_SET){
		return 12 * ball->fontWidth + BAR_WIDTH;
	}

	return 0;
}	

int ULG_BallStatistic::GetHeight()
{
	if (initFlags & ULG_BALLSTATISTIC_F_BALL_SET){
		return ball->ballHeight + 2 * ball->fontHeight;
	}

	return 0;
}
